	// Container 规则解析器
	// 对于同名字段的例如checkbox暂无法解决，暂时解决办法checkbox不同名来解决
	var PatternBuilder=HTMLTurbo.util.PatternBuilder;
	
	var RuleParser=function(selector){
		this.selector=selector;
		this.init()
	}
	RuleParser.prototype.rule=null;
	RuleParser.prototype.classNameRule=null;
	RuleParser.prototype.submitRule=null;
	
	RuleParser.prototype.defaultClassName="";
	
	RuleParser.prototype.init=function(){
		var defaultClassName=$(this.selector).attr("class");
		if(typeof defaultClassName!="undefined"){
			this.defaultClassName=defaultClassName;
		}
	}
	RuleParser.prototype.loadRule=function(url){
		var self=this;
		var options={
			url: url,
			dataType:"json",
			cache:false,
			success: function(data, textStatus, jqXHR){
				self.loadRuleSuccess(data);
			},
			error :function(){
				alert("rule load error ");
			}
		}
		$.ajax(options);
	}
	RuleParser.prototype.loadRuleSuccess=function(data){
		this.setRule(data);
	}
	RuleParser.prototype.setRule=function(rule){
		this.rule=rule;
		this.setClassNameRule(rule.classNameRule);
		this.setSubmitRule(rule.submitRule);
	}
	RuleParser.prototype.bindChange=function(type){
		var self=this;
		var listenFields=this.classNameRule.listenFields;
		for(var i=0;i<listenFields.length;i++){
			var name=listenFields[i];
			$("[name='"+name+"']",this.selector).bind("change",function(event){
				if(type=="className"){
					self.updateClassNameRule();
				}
				if(type=="submit"){
					self.updateSubmitRule();
				}
			})
		}
	}
	RuleParser.prototype.getRuleNameList=function(matchRules,dataList){
		var ruleNames="";
		var ruleNameList=new Array();
		
		var data=new Array();
		var name="";
		var value="";
		
		var matchRule=null;
		
		var evalStr="";
		//console.log(dataList);
		
		for(var i=0;i<dataList.length;i++){
			data=dataList[i];
			name=data.name;
			value=data.value;
			value=value.replace(/\r\n|\r|\n/g,"\\n");
			name=name.replace("[]","");
			evalStr="var "+name+"='"+value+"'";
			//console.log(evalStr);
			eval(evalStr);
		}
		for(var i=0;i<matchRules.length;i++){
			matchRule=matchRules[i]
			evalStr=matchRule.matchStr
			
			//值为空时try
			try{
				if(eval(evalStr)){
					ruleNameList.push(matchRule.name);
				}
			}catch(e){
				//console.log(e)
			}
		}
		return ruleNameList;
	}
	
	
	
	
	
	
	
	
	
	
	/*---className block---*/
	RuleParser.prototype.setClassNameRule=function(classNameRule){
		this.classNameRule=classNameRule
		this.bindChange("className");
		this.updateClassNameRule();
	}
	
	RuleParser.prototype.updateClassNameRule=function(){
		var dataList=this.getDataList();
		var ruleNameList=this.getRuleNameList(this.classNameRule.matchRules,dataList);
		var ruleNames=ruleNameList.join(" ");
		var className="";
		if(this.defaultClassName!=""){
			className=this.defaultClassName+" ";
		}
		className+=ruleNames;
		console.log("className="+className);
		$(this.selector).attr("class",className);
	}
	
	
	/*---submit block---*/
	RuleParser.prototype.setSubmitRule=function(submitRule){
		this.submitRule=submitRule
		this.bindChange("submit");
		this.updateSubmitRule();
	}
	RuleParser.prototype.updateSubmitRule=function(){
		var dataList=this.getDataList();
		var ruleNameList=this.getRuleNameList(this.submitRule.matchRules,dataList);
		var ruleNames=ruleNameList.join(" ");

		console.log("submitRuleNames="+ruleNames);
		
		this.updateSubmitRuleRuleFieldList(ruleNameList);
	}
	
	
	RuleParser.prototype.extend=function(obj1,obj2){
		//console.log(obj1);
		//console.log(obj2);
		for(var i in obj2){
			//不做继承参数判断只继承
			//obj1[i]=obj2[i];
			
			//继承参数做判断
			for(var j in obj1){
				if(obj1[j].name==obj2[i].name){
					obj1[j]=this.extendPara(obj1[j],obj2[i]);
					break;
					//obj1[j]=obj2[i];
					//break;
				}
			}
		}
		return obj1;
	}
	RuleParser.prototype.extendPara=function(obj1,obj2){
		var key="";
		var value="";
		var evalStr="";
		for(var i in obj2){
			key=i;
			evalStr="obj2."+key
			value=eval(evalStr);
			evalStr="obj1."+key+"=value"
			eval(evalStr)
		}
		return obj1;
	}
	RuleParser.prototype.updateSubmitRuleRuleFieldList=function(ruleNameList){
		var ruleGroups=this.submitRule.ruleGroups;
		var ruleGroup=null;
		var ruleGroupList=new Array();
		//console.log(ruleNameList);
		//var resultGroup={};
		
		//规则组列表
		for(j in ruleNameList){
			var ruleName=ruleNameList[j];
			for(i in ruleGroups){
				if(ruleGroups[i].name==ruleName){
					ruleGroup=ruleGroups[i];
					//console.log(ruleGroup);
					ruleGroup=$.extend(true,{},ruleGroup)
					ruleGroupList.push(ruleGroup);
				}
			}
		}
		//console.log(ruleGroupList);
		//字段规则列表
		var fieldRuleList=ruleGroupList[0].fieldRuleList;
		//console.log(fieldRuleList);
		//fieldRuleList=$.extend({},fieldRuleList)
		for(var i=1;i<ruleGroupList.length;i++){
			fieldRuleList=this.extend(fieldRuleList,ruleGroupList[i].fieldRuleList)
		}
		if(fieldRuleList==null){
			return ;
		}
		//var ruleList=this.ruleData.submitRules.base;
		//this.submitRuleList=ruleList;
		//var element=null;
		
		//console.log(fieldRuleList);
		var rule=null;
		for(var i=0;i<fieldRuleList.length;i++){
			fieldRule=fieldRuleList[i];
			this.updateFieldRule(fieldRule);
		}
		this.submitFieldRuleList=fieldRuleList;
	}
	RuleParser.prototype.submitFieldRuleList=null;
	
	RuleParser.prototype.updateFieldRule=function(rule){
		var label=rule.label;
		var name=rule.name;
		var required=rule.required;
		var pattern=rule.pattern;
		var patternName=rule.patternName;
		var placeholder=rule.placeholder;
		var title=rule.title;
		var type=rule.type;
		var tips=rule.tips;
		//console.log(name);
		
		var elements=$("[name=\""+name+"\"]",this.selector);
		
		//生成规则
		if(patternName!=""){
			var evalStr="PatternBuilder."+patternName;
			if(pattern==""){
				pattern=eval(evalStr);
			}
		}
		var isVisible=$(elements).is(":visible")
		//附加规则
		if(pattern!=""){
			if(type!="select"){
				if(isVisible){
					elements.prop("pattern",pattern);
				}
			}
		}else{
			elements.removeAttr("pattern");
		}
		//增加必填
		if(required){
			if(isVisible){
				elements.prop("required","required");
				//console.log(name+" 1");
			}
		}else{
			//console.log(name+" 2");
			elements.removeProp("required");
		}
		
		//增加提示
		if(title){
			elements.attr("title",title);
		}
		//增加提示
		if(placeholder){
			elements.attr("placeholder",placeholder);
		}
		//增加字段名称
		var labelElement=this.getLabelElement(elements);
		if(labelElement){
			var labelHtml="";
			if(required){
				labelHtml+="<span class=\"required\">*</span>";
			}
			labelHtml+=label+"：";
			labelElement.html(labelHtml);
		}
		
		//增加提示
		if(tips!=""){
			var tipsElement=this.getTipsElement(elements);
			if(tipsElement){
				var tipsHtml="";
				tipsHtml=tips;
				tipsElement.html(tipsHtml);
			}
		}
	}
	RuleParser.prototype.getLabelElement=function(elements){
		var labelElement=elements.parent().find("label");
		//console.log(labelElement);
		if(!labelElement){
			//console.log(11);
			labelElement=this.getLabelElement(elements.parent());
		}
		return labelElement;
	}
	RuleParser.prototype.getTipsElement=function(elements){
		var tipsElement=elements.parent().find(".tips");
		if(tipsElement.length==0){
			tipsElement=$("<span class=\"tips\"></span>");
			elements.parent().append(tipsElement);
		}
		return tipsElement;
	}
	
	RuleParser.prototype.getDataList=function(){
		var serializeArray=$(this.selector).serializeArray();
		return serializeArray;
	}
	RuleParser.prototype.validate=function(){
		var dataList=this.getDataList();
		
		var ruleList=this.submitFieldRuleList;
		var rule=null;
		var value=null;
		var target=null;
		
		var name,label,type,required,pattern,patternName,placeholder,title,tips,maxLength;

		
		var result=null;
		var resultList=new Array();
		//console.log(ruleList);
		for(var i=0;i<ruleList.length;i++){
			rule=ruleList[i];
			//console.log(rule);
			
			name=rule.name;
			type=rule.type;
			required=rule.required;
			label=rule.label;
			pattern=rule.pattern;
			patternName=rule.patternName;
			title=rule.title;
			value=this.getValueByName(dataList,name);
			if(typeof value=="undefined"){
				value="";
			}
			//console.log(value);
			target=$("[name="+name+"]",this.element);
			
			result=new Result();
			result.target=name;
			result.tips="";
			result.event=Result.EVENT_SUCCESS;
			
			//跳过源始密码的验证
			if(name=="sourcePassword"){
				continue;
			}
			//事件提示语
			var eventTips="";
			if(required){
				if(type=="select"){
					eventTips=label+" 必选 "+title;
				}else{
					eventTips=label+" 必填 "+title;
				}
			}else{
				eventTips=label+" "+title;
			}
			//必填验证
			if(required){
				if(value==""){
					//console.log(name);
					result.target=name
					result.tips=eventTips
					result.event=Result.EVENT_FAIL;
					resultList.push(result);
					continue;
				}
			}
			//规则验证
			if(patternName!=""){
				var evalStr="PatternBuilder."+patternName;
				if(pattern==""){
					pattern=eval(evalStr);
				}
			}
			if(value!=""){
				if(pattern!=""){
					//if(type!="select"){
						var regExp=new RegExp(pattern);
						//console.log(value);
						//console.log(pattern);
						if(!regExp.test(value)){
							result.target=name
							result.tips=eventTips
							result.event=Result.EVENT_FAIL;
							resultList.push(result);
							continue;
						};
					//}
				}
			}
			//console.log(i);
			resultList.push(result);
		}
		//console.log(resultList);
		
		//最后结果
		result=new Result();
		result.event=Result.EVENT_SUCCESS
		result.resultList=resultList;
		this.validateResult=result;
		
		for(var i=0;i<resultList.length;i++){
			if(resultList[i].event==Result.EVENT_FAIL){
				result.event=Result.EVENT_FAIL
				break;
			}
		}
		if(result.event==Result.EVENT_SUCCESS){
			return true;	
		}else{
			return false;
		}
	}
	RuleParser.prototype.getValueByName=function(dataList,name){
		var value;
		var data;
		for(var i=0;i<dataList.length;i++){
			data=dataList[i]
			if(data.name==name){
				value=data.value;
			}
		}
		return value;
	}
	
	
	
	var Result=function(){}
	Result.EVENT_SUCCESS="eventSuccess";
	Result.EVENT_FAIL="eventFail";
	Result.prototype.target=""
	Result.prototype.action=""
	Result.prototype.event=""
	Result.prototype.tips=""
	Result.prototype.resultList=null
	Result.prototype.getResultListFailTips=function(){
		var resultList=this.resultList;
		var tips="";
		for(var i=0;i<resultList.length;i++){
			if(resultList[i].event==Result.EVENT_FAIL){
				tips+=resultList[i].tips+"\n";
			}
		}
		return tips;
	}
	HTMLTurbo.util.RuleParser=RuleParser;